package com.example.concurrentprogramming8016.thread.Atomic;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author liyanhao
 * @Date 2023-01-03 19:14
 */
public class AtomicReferenceTest {
    public static void main(String[] args) {
        User user1 = new User("张三", 23);
        User user2 = new User("李四", 25);
        User user3 = new User("王五", 20);

        //初始化为user1
        AtomicReference<User> userAtomicReference = new AtomicReference<>();
        userAtomicReference.set(user1);
        System.out.println(userAtomicReference.get());
        //把user2赋值给userAtomicReference
        userAtomicReference.compareAndSet(user1, user2);
        System.out.println(userAtomicReference.get());
        //把user3 赋值给userAtomicReference
        userAtomicReference.compareAndSet(user2, user3);
        System.out.println(userAtomicReference.get());
    }
}

@Data
@AllArgsConstructor
class User{
    private String name;
    private Integer age;
}
